const { ccclass, property } = cc._decorator;

const BLUR_BASE_RADIUS = 8;

@ccclass
export default class GussainBlur extends cc.Component {

    @property
    blurRadius: number = 4;
    @property
    dura: number = .2;

    _curSpr: cc.Sprite = null;
    _blurMat: cc.Material = null;

    onLoad() {
        this._curSpr = this.node.getComponent(cc.Sprite);
        this._blurMat = this._curSpr.getMaterial(0);
    }

    onEnable() {
        // this.updateBlur();
    }

    updateBlur() {
        this._blurMat = this.node.getComponent(cc.Sprite).getMaterial(0);
        let res = this._blurMat.getProperty("textureSize", 0);
        let textureSize = cc.v2(res[0], res[1]);
        let data = { val: BLUR_BASE_RADIUS / this.blurRadius * 8 };
        cc.tween(data).to(this.dura, { val: BLUR_BASE_RADIUS / this.blurRadius }, {
            progress: (start, end, current, ratio) => {
                let val = cc.misc.lerp(start, end, ratio);
                this._blurMat.setProperty("textureSize", textureSize.mul(val));
                return val;
            }
        }).start();
    }
}
